<?php
declare (strict_types=1);

namespace app\controller;

use app\facade\Param;
use app\handler\Aes;
use app\handler\Excel;
use app\helpers\api\ApiResponse;
use app\model\Company;
use think\Request;

class CompanyController
{
    use ApiResponse;

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {   
    
        $query = Company::order('create_time', 'desc');
        $query = Param::companyParam($request->param(), $query);
        return $this->success($query->paginate($request->param('page_size', 15)));
    }

    /**
     * 详情
     * @param Company $company
     * @return mixed
     */
    public function show(Company $company, Aes $aes)
    {
        $company->password = $aes->decode($company->password);
        // dd($company->password);
        return $this->success($company);
    }

    /**
     * @param Request $request
     * @return mixed
     */
    public function store(Request $request, Aes $aes)
    {
        $validate = new \app\validate\Company();
        $res      = $validate->check($request->param());
        if (!$res) {
            return $this->error($validate->getError());
        }
        $data = $request->param();
        if ($request->param('password')) $data['password'] = $aes->encode($request->param('password'));
        Company::create($data);
        return $this->created();
    }

    /**
     * 保存新建的资源
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request, Company $company, Aes $aes)
    {
        $data = $request->param();
        if ($request->param('password')) $data['password'] = $aes->encode($request->param('password'));
        $company->update($data);
        return $this->update();
    }

    public function delete(Company $company)
    {
        $company->delete();
        return $this->destroy();
    }

    public function exportTemplate()
    {
        $datas = [
            [
                '企业名称',
                '电子邮箱',
                '联系人',
                '电话',
                '手机',
                '地址',
                '统一社会信用代码',
                '网址'
            ]
        ];
        $excel = new Excel();
        $excel->exportExcel($datas, '企业模板');
    }

    public function exportData(Request $request)
    {
        $key   = [
            '企业名称',
            '电子邮箱',
            '联系人',
            '电话',
            '手机',
            '地址',
            '统一社会信用代码',
            '网址'
        ];
        $query = Company::field('name,email,contactPerson,telephone,phone,address,unifiedSocialCreditCode,URL')->order('id', 'desc');
        $query = Param::companyParam($request->param(), $query);
        $datas = $query->select()->toArray();
        array_unshift($datas, $key);
        $excel = new Excel();
        $excel->exportExcel($datas, '企业数据');
    }

    public function import(Request $request, Aes $aes)
    {
        $file = request()->file('exl');
        // 上传到本地服务器
        $savename = \think\facade\Filesystem::disk('public')->putFile('exl', $file);
        $exl      = new Excel();
        $data     = $exl->importExecl(app()->getRootPath() . 'init/storage/' . $savename);
        unset($data[1]);
        foreach ($data as $k => $value) {
            $exist = Company::where('name', $value['A'])->findOrEmpty();
            if (!$exist->isEmpty()) {
                continue;
            }
            Company::create([
                'name'                    => $value['A'],
                'email'                   => $value['B'],
                'contactPerson'           => $value['C'],
                'telephone'               => $value['D'],
                'phone'                   => $value['E'],
                'address'                 => $value['F'],
                'unifiedSocialCreditCode' => $value['G'],
                'URL'                     => $value['H'],
                'state'                   => 1,
                'password'                => $aes->encode(123456),
            ]);
        }
        unlink(app()->getRootPath() . 'init/storage/' . $savename);
        return $this->created();
    }
}
